home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 09 - QuickDraw 3D / TextureMap / Setup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  10.1 KB  |  374 lines  |  [TEXT/MPCC]

  1. /****************************/
  2. /*       SETUP.C                */
  3. /****************************/
  4.  
  5.  
  6. /****************************/
  7. /*    EXTERNALS             */
  8. /****************************/
  9.  
  10. #include <qd3d.h>
  11. #include <QD3DView.h>
  12. #include <QD3DDrawContext.h>
  13. #include <QD3DCamera.h>
  14. #include <QD3DLight.h>
  15. #include <QD3DShader.h>
  16. #include <QD3DGroup.h>
  17. #include <QD3DRenderer.h>
  18.  
  19. /****************************/
  20. /*    PROTOTYPES            */
  21. /****************************/
  22.  
  23. extern    void DoFatalAlert(Str255 s);
  24.  
  25. extern    void LoadMyTextureMap(void);
  26. extern    void MakeTexturedPolygonGroup(void);
  27.  
  28.  
  29. void InitMyScene(void);
  30. TQ3DrawContextObject CreateMyDrawContex(WindowPtr theWindow);
  31. void CreateMyStylesAndShader(void);
  32. TQ3GroupObject CreateMyLightGroup(void);
  33. TQ3CameraObject CreateMyCamera(TQ3DrawContextObject drawContext);
  34. void CreateMyView(void);
  35.  
  36. /****************************/
  37. /*    CONSTANTS             */
  38. /****************************/
  39.  
  40.  
  41. #define    MY_WIND_ID        400
  42.  
  43. /*********************/
  44. /*    VARIABLES      */
  45. /*********************/
  46.  
  47. WindowPtr            gMyWindow;
  48.  
  49. TQ3CameraObject        gMyCameraObject;
  50. TQ3ViewObject        gMyViewObject;
  51. TQ3StyleObject        gMyStyleObject_Interpolation;
  52. TQ3StyleObject        gMyStyleObject_Backfacing;
  53. TQ3StyleObject        gMyStyleObject_Fillstyle;
  54. TQ3ShaderObject        gMyShaderObject_IlluminationShader;
  55.  
  56.  
  57.  
  58. /************** INIT MY SCENE *******************/
  59. //
  60. // Does everything needed to prepare a window for rendering
  61. //
  62.  
  63. void InitMyScene(void)
  64. {
  65.                 /* GET THE WINDOW TO USE */
  66.  
  67.     gMyWindow = GetNewCWindow(MY_WIND_ID,nil,(WindowPtr)-1L);    // get from resource        
  68.     if (gMyWindow == nil)
  69.         DoFatalAlert("\pWhere did the window go?");                
  70.     SetPort    (gMyWindow);
  71.  
  72.     CreateMyView();                    // create view object
  73.     CreateMyStylesAndShader();        // create styles & shader objects
  74.     
  75.     LoadMyTextureMap();                // create texture shader object
  76.     MakeTexturedPolygonGroup();        // create a polygon geometry & a group
  77. }
  78.  
  79.  
  80. /******************* CREATE MY VIEW *************************/
  81. //
  82. // This creates our view object.
  83. //
  84. // INPUT    :    none
  85. // OUTPUT    :    none
  86. //
  87. // NOTE: gMyViewObject is a global variable of type TQ3ViewObject
  88. //
  89.  
  90. void CreateMyView(void)
  91. {
  92. TQ3Status                myErr;
  93. TQ3DrawContextObject    myDrawContext;
  94. TQ3RendererObject        myRenderer;
  95. TQ3GroupObject            myLights;
  96.     
  97.     
  98.                 /* CREATE NEW VIEW OBJECT */
  99.                 
  100.     gMyViewObject = Q3View_New();
  101.     if (gMyViewObject == nil)
  102.         DoFatalAlert("\pQ3View_New failed!");
  103.  
  104.     
  105.             /* CREATE A DRAW CONTEXT OBJECT */
  106.     
  107.     myDrawContext = CreateMyDrawContex(gMyWindow);
  108.     if (myDrawContext == nil)
  109.         DoFatalAlert("\pMyNewDrawContext Failed!");
  110.  
  111.  
  112.             /* ASSIGN DRAW CONTEXT TO THE VIEW */
  113.                 
  114.     myErr = Q3View_SetDrawContext(gMyViewObject, myDrawContext);
  115.     if ( myErr == kQ3Failure )
  116.         DoFatalAlert("\pSetDrawContext Failed!");
  117.  
  118.  
  119.                 /* CREATE RENDERER OBJECT */
  120.             
  121.     myRenderer = Q3Renderer_NewFromType(kQ3RendererTypeInteractive);
  122.     if (myRenderer == nil)
  123.         DoFatalAlert("\pRenderer_NewFromType Failed!");
  124.  
  125.  
  126.             /* ASSIGN RENDERER TO THE VIEW */
  127.  
  128.     myErr = Q3View_SetRenderer(gMyViewObject, myRenderer);
  129.     if ( myErr == kQ3Failure )
  130.         DoFatalAlert("\pSetRenderer Failed!");
  131.  
  132.  
  133.             /* CREATE A CAMERA OBJECT */
  134.  
  135.     gMyCameraObject = CreateMyCamera(myDrawContext);
  136.     if (gMyCameraObject == nil)
  137.         DoFatalAlert("\pCreateMyCamera Failed!");
  138.  
  139.  
  140.             /* ASSIGN CAMERA TO THE VIEW */
  141.                     
  142.     myErr = Q3View_SetCamera(gMyViewObject, gMyCameraObject);
  143.     if ( myErr == kQ3Failure )
  144.         DoFatalAlert("\pSetCamera Failed!");
  145.  
  146.  
  147.             /* CREATE A LIGHT GROUP OBJECT */
  148.             
  149.     myLights = CreateMyLightGroup();
  150.     if ( myLights == nil )
  151.         DoFatalAlert("\pCreateMyLightGroup Failed!");
  152.         
  153.         
  154.         /* ASSIGN LIGHT GROUP TO THE VIEW */
  155.         
  156.     myErr = Q3View_SetLightGroup(gMyViewObject, myLights);
  157.     if ( myErr == kQ3Failure )
  158.         DoFatalAlert("\pSetLightGroup Failed!");
  159. }
  160.  
  161.  
  162. /**************** CREATE MY DRAW CONTEXT *********************/
  163. //
  164. // Creates a DRAW CONTEX which will be attached to the flythru VIEW object.
  165. // The draw context defines how a frame is drawn to a window or offscreen buffer.
  166. //
  167. // INPUT    :    pointer to the window we will be drawing in
  168. // OUTPUT    :    the new Draw Object
  169. //
  170.  
  171. TQ3DrawContextObject CreateMyDrawContex(WindowPtr theWindow)
  172. {
  173. TQ3DrawContextObject    myDrawContext;
  174. TQ3DrawContextData        myDrawContextData;
  175. TQ3MacDrawContextData    myMacDrawContextData;
  176. TQ3ColorARGB            clearColor = {1,0,0,0.25};            // dark blue
  177.  
  178.  
  179.             /* FILL IN DRAW CONTEXT DATA */
  180.  
  181.     myDrawContextData.clearImageMethod = kQ3ClearMethodWithColor;    // how to clear
  182.     myDrawContextData.clearImageColor = clearColor;                    // color to clear with
  183.     myDrawContextData.pane.min.x = (theWindow->portRect).left;        // set bounds
  184.     myDrawContextData.pane.max.x = (theWindow->portRect).right;
  185.     myDrawContextData.pane.min.y = (theWindow->portRect).top;
  186.     myDrawContextData.pane.max.y = (theWindow->portRect).bottom;
  187.     myDrawContextData.paneState = kQ3True;                            // use bounds = yes
  188.     myDrawContextData.maskState = kQ3False;                            // no mask
  189.     myDrawContextData.doubleBufferState = kQ3True;                    // use double buffering
  190.  
  191.  
  192.                 /* SET MAC SPECIFIC DATA */
  193.                 
  194.     myMacDrawContextData.drawContextData = myDrawContextData;
  195.     myMacDrawContextData.window = (CWindowPtr)theWindow;            // set window to draw into
  196.     myMacDrawContextData.library = kQ3Mac2DLibraryNone;
  197.     myMacDrawContextData.viewPort = nil;                            // (for GX only)
  198.     myMacDrawContextData.grafPort = nil;
  199.  
  200.  
  201.         /* CREATE MACINTOSH DRAW CONTEXT BASED ON THAT DATA */
  202.  
  203.     myDrawContext = Q3MacDrawContext_New(&myMacDrawContextData);
  204.     if (myDrawContext == nil)
  205.         DoFatalAlert("\pQ3MacDrawContext_New failed");
  206.  
  207.     return (myDrawContext);
  208. }
  209.  
  210.  
  211. /****************** CREATE MY CAMERA *********************/
  212. //
  213. // Creates a camera which will be attached to the VIEW object.
  214. //
  215. // INPUT    :    the draw context object
  216. // OUTPUT    :     the camera Object
  217. //
  218.  
  219. TQ3CameraObject CreateMyCamera(TQ3DrawContextObject drawContext)
  220. {
  221. TQ3CameraObject                    myCamera;
  222. TQ3CameraData                    myCameraData;
  223. TQ3ViewAngleAspectCameraData    myViewAngleCameraData;
  224. TQ3Point3D                        cameraFrom = {0.0, 20.0, 15.0};
  225. TQ3Point3D                        cameraTo = {0.0, 0.0, 0.0};
  226. TQ3Vector3D                        cameraUp = {0.0, 1.0, 0.0};
  227. TQ3Area                            pane;
  228.  
  229.  
  230.                 /* FILL IN CAMERA DATA */
  231.                 
  232.     myCameraData.placement.cameraLocation = cameraFrom;            // set camera coords
  233.     myCameraData.placement.pointOfInterest = cameraTo;            // set "looking at" coords
  234.     myCameraData.placement.upVector = cameraUp;                    // set a vector that's "up"
  235.     myCameraData.range.hither = 0.1;                            // set frontmost Z dist
  236.     myCameraData.range.yon = 100.0;                                // set farthest Z dist
  237.     myCameraData.viewPort.origin.x = -1.0;                        // set view origins
  238.     myCameraData.viewPort.origin.y = 1.0;
  239.     myCameraData.viewPort.width = 2.0;
  240.     myCameraData.viewPort.height = 2.0;
  241.  
  242.     myViewAngleCameraData.cameraData = myCameraData;
  243.     myViewAngleCameraData.fov = 1.1;                            // set field of view angle
  244.     Q3DrawContext_GetPane(drawContext,&pane);                    // get window pane info
  245.     myViewAngleCameraData.aspectRatioXToY =                      // set aspect ratio of window
  246.             (pane.max.x-pane.min.x)/(pane.max.y-pane.min.y);
  247.  
  248.  
  249.         /* CREATE THE NEW CAMERA OBJECT BASED ON THAT DATA */
  250.                 
  251.     myCamera = Q3ViewAngleAspectCamera_New(&myViewAngleCameraData);
  252.  
  253.     return ( myCamera );
  254. }
  255.  
  256.  
  257.  
  258. /********************* CREATE MY LIGHT GROUP ************************/
  259. //
  260. // Creates a light group which will be attached to the VIEW object.
  261. //
  262. // INPUT    : none
  263. // OUTPUT    : the new Light Group Object
  264. //
  265.  
  266. TQ3GroupObject CreateMyLightGroup(void)
  267. {
  268. TQ3GroupPosition        myGroupPosition;
  269. TQ3GroupObject            myLightGroup;
  270. TQ3LightData            myLightData;
  271. TQ3DirectionalLightData    myDirectionalLightData;
  272. TQ3LightObject            myAmbientLight, myFillLight;
  273. TQ3Vector3D                fillDirection = { -0.7, -1.0, 0 };
  274. TQ3ColorRGB                whiteLight = { 1.0, 1.0, 1.0 };
  275.  
  276.     myLightData.isOn = kQ3True;                                    // light is ON
  277.     myLightData.color = whiteLight;                                // set color of light
  278.  
  279.             /* CREATE AMBIENT LIGHT */
  280.  
  281.     myLightData.brightness = 0.7;                                // set brightness value
  282.     myAmbientLight = Q3AmbientLight_New(&myLightData);            // make new light
  283.     if (myAmbientLight == nil)
  284.         DoFatalAlert("\pQ3AmbientLight_New Failed!");
  285.     
  286.  
  287.             /* CREATE DIRECTIONAL LIGHT */
  288.             
  289.     myLightData.brightness = 2.0;                                // set brightness
  290.     myDirectionalLightData.lightData = myLightData;                // refer to general light info
  291.     myDirectionalLightData.castsShadows = kQ3False;                // no shadows
  292.     myDirectionalLightData.direction = fillDirection;            // set fill direction vector
  293.     myFillLight = Q3DirectionalLight_New(&myDirectionalLightData);    // make new light object
  294.     if (myFillLight == nil)
  295.         DoFatalAlert("\pQ3DirectionalLight_New Failed!");
  296.  
  297.  
  298.             /* CREATE LIGHT GROUP */
  299.  
  300.     myLightGroup = Q3LightGroup_New();
  301.     if ( myLightGroup == nil )
  302.         DoFatalAlert("\pQ3LightGroup_New Failed!");
  303.  
  304.  
  305.         /* ADD AMBIENT LIGHT OBJECT TO THE GROUP */
  306.  
  307.     myGroupPosition = Q3Group_AddObject(myLightGroup, myAmbientLight);
  308.     if ( myGroupPosition == 0 )
  309.         DoFatalAlert("\p Q3Group_AddObject Failed!");
  310.         
  311.         
  312.         /* ADD DIRECTIONAL LIGHT OBJECT TO THE GROUP */
  313.         
  314.     myGroupPosition = Q3Group_AddObject(myLightGroup, myFillLight);
  315.     if ( myGroupPosition == 0 )
  316.         DoFatalAlert("\p Q3Group_AddObject Failed!");
  317.  
  318.  
  319.         /* DISPOSE OF OUR REFERENCES TO THE LIGHT OBJECTS */
  320.         
  321.     Q3Object_Dispose(myFillLight);    
  322.     Q3Object_Dispose(myAmbientLight);    
  323.  
  324.     return ( myLightGroup );    
  325. }
  326.  
  327.  
  328. /**************** CREATE MY STYLES AND SHADER ****************/
  329. //
  330. // Creates style objects which define how the scene is to be rendered.
  331. // It also sets the shader object.
  332. //
  333. // INPUT    :    none
  334. // OUTPUT    :    none
  335. //
  336.  
  337. void CreateMyStylesAndShader(void)
  338. {
  339.  
  340.                     /* SET INTERPOLATION (FOR SHADING) */
  341.                     
  342.     gMyStyleObject_Interpolation =                             // do vertex interpolation
  343.                 Q3InterpolationStyle_New(kQ3InterpolationStyleVertex);
  344.     if (gMyStyleObject_Interpolation == nil )
  345.         DoFatalAlert("\p Q3InterpolationStyle_New Failed!");
  346.  
  347.  
  348.                     /* SET BACKFACING */
  349.  
  350.     gMyStyleObject_Backfacing =                             // remove backfaces
  351.         Q3BackfacingStyle_New(kQ3BackfacingStyleRemove);
  352.     if (gMyStyleObject_Backfacing == nil )
  353.         DoFatalAlert("\p Q3BackfacingStyle_New Failed!");
  354.  
  355.  
  356.                 /* SET POLYGON FILL STYLE */
  357.                         
  358.     gMyStyleObject_Fillstyle =                                 // we want the polys filled
  359.         Q3FillStyle_New(kQ3FillStyleFilled);
  360.     if (gMyStyleObject_Fillstyle == nil )
  361.         DoFatalAlert("\p Q3FillStyle_New Failed!");
  362.  
  363.  
  364.                 /* SET SHADER TO LAMBERT */
  365.  
  366.     gMyShaderObject_IlluminationShader = Q3LambertIllumination_New();
  367.     if (gMyShaderObject_IlluminationShader == nil )
  368.         DoFatalAlert("\p Q3LambertIllumination_New Failed!");
  369. }
  370.  
  371.  
  372.  
  373.  
  374.